package com.base.web.core.config.swagger2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Created with IntelliJ IDEA.
 * Description:  swagger2 配置类
 *
 * @Author: RenTie
 * @Date: 2018-08-16 9:35
 **/


@Configuration
@EnableSwagger2 //注解开启 swagger2 功能
public class Swagger2Config {

    @Autowired
    private Swagger2ApiInfo info;

    /**
     * 获取配置文件的文档配置信息
     *
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "swagger2-api.info")
    public Swagger2ApiInfo getApiInfo() {
        return new Swagger2ApiInfo();
    }

    /**
     * 构建一个Docket类
     * @return
     */
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SPRING_WEB.SWAGGER_2)
                .apiInfo(apiInfo())//加载api信息
                .select()
                .apis(RequestHandlerSelectors.basePackage(info.getPath()))//选择需要监控的包
                .paths(PathSelectors.any())//对所有路径进行监控
                .build();
    }

    /**
     * 构建 api文档的详细信息
     * 这里配置的信息都会显示在页面上
     * @return
     */
    public ApiInfo apiInfo(){

        return new ApiInfoBuilder()
                .title(info.getTitle())//页面标题
                .description(info.getDescription())//文档描述
                .contact(new Contact(info.getName(),info.getUrl(),info.getEmail()))//创建人信息
                .version(info.getVersion())//版本
                .build();
    }
}
